//
//  ViewController.m
//  NSFileManager&NSFileHandle
//
//  Created by apple on 15/8/18.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
#if 0
    //文件夹里面还可以写其他的东西，但是文件就只能写内容；
    NSString *filePath = [NSHomeDirectory()
                          stringByAppendingPathComponent:@"Documents"];
    //注：通常情况下，documents目录是可以存放在用户需要持久化的数据的，但是如果该应用支持文件共享，那么用户可以通过iTunes查看和修改应用的documents目录，所以此时应用的隐私文件不应该放到此目录下！每个应用在自己的沙盒中有自己私有的Application Support 目录，我们可以将隐私文件放入其中［该目录可以一开始并不存在，我们可以创建并获取它］。
#endif
    
#if 0
    
    //在沙盒Documents目录下，创建一个文件夹demoFileDir;
    
    //1、读取根目录，并与后面的字符串相连，组成一个新的字符串；
    NSString *filePath =[NSHomeDirectory()
        stringByAppendingPathComponent:@"Documents/demoFileDir"];
    NSLog(@"filePath-->%@",filePath);//       这里的filePath是我们所创建的文件夹多地址；
   
    //创建一个NSFileManager的对象；
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    //指定文件夹的路径，创建文件夹；
    [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
#endif
    
#if 0
    //创建文件；
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
   
    NSString *str = [NSHomeDirectory()
        stringByAppendingPathComponent:@"Documents/abc"];
    
    [fileManager createFileAtPath:str contents:[@"Hello World" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    NSLog(@"str-->%@",str);
    NSLog(@"error-->%@",[error localizedDescription]);
#endif

#if 0
    //创建文件
    NSString *str = [NSHomeDirectory()
                     stringByAppendingPathComponent:@"Documents/a"];
//    NSFileManager *fielManager = [NSFileManager defaultManager];
    
    NSError *error;
    
    //必须要指定文件的名称（路径）；
    [@"Hello" writeToFile:str atomically:YES encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"str-->%@",str);
    NSLog(@"error-->%@",[error localizedDescription]);
    
#endif

#if 0
    NSString *str = [NSHomeDirectory()
                     stringByAppendingPathComponent:@"Documents/a"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    //创建文件；
//    [fileManager createFileAtPath:str contents:[@"hello world" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    
    //创建文件夹；
    [fileManager createDirectoryAtPath:str withIntermediateDirectories:NO attributes:nil error:nil];
    NSLog(@"str-->%@",str);
#endif

#if 0
    NSString *str = [NSHomeDirectory()
                     stringByAppendingPathComponent:@"Documents/b"];
    NSFileManager *filaManager = [NSFileManager defaultManager];
    [filaManager createFileAtPath:str contents:[@"fuck a bitch" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    
    //--------------------------------------------------------------------------------------------------------------
    
    NSString *string = [NSString stringWithContentsOfFile:str encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"文件长度--->%ld",string.length);   //这个位置是打印长度，记得输出格式符；
   //--------------------------------------------------------------------------------------------------------------
    
    BOOL a;
    [filaManager fileExistsAtPath:str isDirectory:&a];//用以判断文件是否已存在，且是否为文件夹；
    //--------------------------------------------------------------------------------------------------------------
    
    if ([filaManager fileExistsAtPath:str])
    {
        NSLog(@"文件存在");
    }
    else
    {
        NSLog(@"文件不存在");
    }
#endif
    
#if 0  
    //  有点小看不懂呢！！！转换成c语言输出？----------------------------------------------------------------------------------------
    NSString *filePath = [NSHomeDirectory()
        stringByAppendingPathComponent:@"Documents/abc"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSDictionary *dict = [fileManager attributesOfItemAtPath:filePath error:nil];
    
    unsigned long long fileSize = [dict fileSize];
    NSLog(@"文件的大小-->%llu",fileSize);
    NSLog(@"dict-->%@",dict);
    NSLog(@"filePath-->%@",filePath);
    //--------------------------------------------------------------------------------------------------------------
#endif
    
#if 0
    ///?它这一步有什么作用啊。。。
    NSString *filePath = [NSHomeDirectory()
                          stringByAppendingPathComponent:@"Documents/a"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error];/////////////////
    ///////////
    NSString *filePath1 = [NSHomeDirectory()
                           stringByAppendingPathComponent:@"Documents/a/b"];
    [fileManager createFileAtPath:filePath1 contents:[@"hello world" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    NSLog(@"error--> %@",error);
    NSLog(@"filePath-->%@",filePath);
#endif
    
#if 0
    //--------------------------------------------------------------------------------------------------------------
    NSString *filePath = [NSHomeDirectory()
                          stringByAppendingPathComponent:@"Documents/a"];
    NSString *filePath1 = [NSHomeDirectory()
                          stringByAppendingPathComponent:@"Documents/b"];
    NSString *filePath2 = [NSHomeDirectory()
                          stringByAppendingPathComponent:@"Documents/c"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [@"a" writeToFile:filePath atomically:NO encoding:NSUTF8StringEncoding error:nil];
    [@"b" writeToFile:filePath1 atomically:NO encoding:NSUTF8StringEncoding error:nil];
    [@"c" writeToFile:filePath2 atomically:NO  encoding:NSUTF8StringEncoding  error:nil];
    
    //把一个文件拷贝到另一个文件；？
    [fileManager copyItemAtPath:filePath toPath:filePath1 error:nil];
    NSLog(@"filePath-->%@",filePath);
    
    //移动文件，［同级目录下的移动文件就相当于对文件进行重命名］；
    [fileManager moveItemAtPath:filePath toPath:filePath2 error:nil];
    
//--------------------------------------------------------------------------------------------------------------------
#endif
    
#if 1
    
#endif
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
